package com.lancoo.examview.exam;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.ExamViewBean;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.examview.R;
import com.lancoo.examview.exam.bean.LimitEventBean;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by pengmi on 2022/2/23 15:39
 * Description:
 */
public class ExamPaperUtils {

    private static int isStartChoice=0;
    private static int startLimitTypeIndex,startLimitQuesIndex,endLimitTypeIndex,endLimitQuesIndex;

    public static void showLimitAnswerQuesAreaDialog(Context mContext,int startTypeIndex,
                                                     int startQuesIndex,int endTypeIndex,int endQuesIndex){
        startLimitTypeIndex=startTypeIndex;
        startLimitQuesIndex=startQuesIndex;
        endLimitTypeIndex=endTypeIndex;
        endLimitQuesIndex=endQuesIndex;
        ExamViewBean constantExamBean = ConstantBean.Companion.getConstantExamBean();
        if(constantExamBean==null){
            return;
        }
        List<Type> typeList = constantExamBean.getTypeList();
        if(typeList==null){
            return;
        }
        AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_limit, null);
        builder.setView(inflate);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        final QueseAdapter mAdapter=new QueseAdapter(typeList);
        mAdapter.setLimitIndex(startTypeIndex,startQuesIndex,endTypeIndex,endQuesIndex);
        RecyclerView recyclerView=inflate.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
        isStartChoice=0;
        RadioGroup radioGroup = inflate.findViewById(R.id.rg_limit);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if(checkedId==R.id.rb_1){
                isStartChoice=0;
            }else{
                isStartChoice=1;
            }
        });
        mAdapter.setCallCallBack((typeIndex, quesIndex) -> {
            if(isStartChoice==0){
                if(typeIndex==endLimitTypeIndex){
                    if(quesIndex>endLimitQuesIndex){
                        Toast.makeText(mContext,"开始位置不能大于结束位置",Toast.LENGTH_SHORT).show();
                        return;
                    }

                }else if(typeIndex>endLimitTypeIndex){
                    Toast.makeText(mContext,"开始位置不能大于结束位置",Toast.LENGTH_SHORT).show();
                    return;
                }
            }else{
                if(startLimitTypeIndex==typeIndex){
                    if(startLimitQuesIndex>quesIndex){
                        Toast.makeText(mContext,"开始位置不能大于结束位置",Toast.LENGTH_SHORT).show();
                        return;
                    }

                }else if(startLimitTypeIndex>typeIndex){
                    Toast.makeText(mContext,"开始位置不能大于结束位置",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            if(isStartChoice==0){
                startLimitTypeIndex=typeIndex;
                startLimitQuesIndex=quesIndex;
            }else{
                endLimitTypeIndex=typeIndex;
                endLimitQuesIndex=quesIndex;
            }
            mAdapter.setLimitIndex(startLimitTypeIndex,startLimitQuesIndex,endLimitTypeIndex,endLimitQuesIndex);
            mAdapter.notifyDataSetChanged();
        });

        inflate.findViewById(R.id.tv_sure).setOnClickListener(v -> {
            alertDialog.dismiss();
            LimitEventBean bean=new LimitEventBean();
            bean.setStartLimitTypeIndex(startLimitTypeIndex);
            bean.setStartLimitQuesIndex(startLimitQuesIndex);
            bean.setEndLimitTypeIndex(endLimitTypeIndex);
            bean.setEndLimitQuesIndex(endLimitQuesIndex);
            EventBus.getDefault().post(bean);
        });


    }
}
